﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Weight.Base.Utility
{
    /// <summary>
    /// 时间转换器
    /// </summary>
    public  class TimerConvertHelper
    {
        private  string TodayStartTime = " 00:00:00";
        private string TodayEndTime = " 59:59:59";
        private string DateFormat = "yyyy-MM-dd";
        public TimePoint GetTimePoint(string timeFlag)
        {
            DateTime now = DateTime.Now;
            string year = now.ToString("yyyy");
            TimePoint point = new TimePoint();
            if (timeFlag == "今天")
            {
                point.StartTime = now.ToString(DateFormat);
                point.EndTime = now.ToString(DateFormat);
            }
            else if(timeFlag =="昨天")
            {
                point.StartTime = now.AddDays(-1).ToString(DateFormat);
                point.EndTime = now.AddDays(-1).ToString(DateFormat) ;
            }
            else if(timeFlag =="本周")
            {
                DateTime monday = GetThisWeekMonday();
                point.StartTime = monday.ToString(DateFormat);
                point.EndTime = monday.AddDays(6).ToString(DateFormat);
            }
            else if(timeFlag =="本月")
            {
                point.StartTime = now.AddDays(1 - DateTime.Now.Day).Date.ToString(DateFormat);
                point.EndTime = now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1).ToString(DateFormat);
            }
            else if (timeFlag == "今年")
            {
                DateTime tmp1 = Convert.ToDateTime(now.ToString("yyyy") + "-01-01");
                point.StartTime = tmp1.ToString("yyyy-MM-dd");
                point.EndTime = tmp1.AddMonths(11).AddDays(30).ToString(DateFormat);
            }
            else if (timeFlag == "本季度")
            {
                point = GetQuarter();
            }
            else if (timeFlag == "第一季度")
            {
                point.StartTime = year + "-01-01";
                point.EndTime = year + "-03-31";
            }
            else if (timeFlag == "第二季度")
            {
                point.StartTime = year + "-04-01";
                point.EndTime = year + "-06-30";
            }
            else if (timeFlag == "第三季度")
            {
                point.StartTime = year + "-07-01";
                point.EndTime = year + "-09-30";
            }
            else if (timeFlag == "第四季度")
            {
                point.StartTime = year + "-10-01";
                point.EndTime = year + "-12-31";
            }
            else
            {
                point.StartTime = DateTime.Now.ToString(DateFormat);
                point.EndTime = DateTime.Now.ToString(DateFormat);
            }

            return point;
        }

        /// <summary>
        /// 获取当前季度开始、结束时间点
        /// </summary>
        /// <returns></returns>
        public TimePoint GetQuarter()
        {
            int month = DateTime.Now.Month;
            TimePoint timePoint = new TimePoint();
            string year = DateTime.Now.ToString("yyyy");
            if (month >= 1 && month <= 3)
            {
                timePoint.StartTime = year + "-01-01";
                timePoint.EndTime = year + "-03-31";
            }
            else if(month >=4 && month <=6)
            {
                timePoint.StartTime = year + "-04-01";
                timePoint.EndTime = year + "-06-30";
            }
            else if (month >= 7 && month <= 9)
            {
                timePoint.StartTime = year + "-07-01";
                timePoint.EndTime = year + "-09-30";
            }
            else if (month >= 10 && month <= 12)
            {
                timePoint.StartTime = year + "-10-01";
                timePoint.EndTime = year + "-12-31";
            }
            else
            {
                timePoint.StartTime = DateTime.Now.ToString(DateFormat);
                timePoint.EndTime = DateTime.Now.ToString(DateFormat);
            }

            return timePoint;
        }

        /// <summary>
        /// 获取本周的周一日期
        /// </summary>
        /// <returns></returns>
        public DateTime GetThisWeekMonday()
        {
            DateTime date = DateTime.Now;
            DateTime firstDate = System.DateTime.Now;
            switch (date.DayOfWeek)
            {
                case System.DayOfWeek.Monday:
                    firstDate = date;
                    break;
                case System.DayOfWeek.Tuesday:
                    firstDate = date.AddDays(-1);
                    break;
                case System.DayOfWeek.Wednesday:
                    firstDate = date.AddDays(-2);
                    break;
                case System.DayOfWeek.Thursday:
                    firstDate = date.AddDays(-3);
                    break;
                case System.DayOfWeek.Friday:
                    firstDate = date.AddDays(-4);
                    break;
                case System.DayOfWeek.Saturday:
                    firstDate = date.AddDays(-5);
                    break;
                case System.DayOfWeek.Sunday:
                    firstDate = date.AddDays(-6);
                    break;
            }
            return firstDate;
        }
    }



    public class TimePoint
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public string StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public string EndTime { get; set; }
    }
}
